var swiper = new Swiper('.swiper-container', {
    pagination: '.swiper-pagination',
    paginationClickable: true,
    autoplay: 3000,
});

var dataRoot = document.getElementById("myBody").dataset.root;
var imgId = "";
var imgMsg= "";
var actid = document.getElementById("myBody").dataset.actid;
var userid = document.getElementById("myBody").dataset.userid;
var openid=document.getElementById("myBody").dataset.openid;
var sub = document.getElementById("myBody").dataset.sub;
var homeurl = document.getElementById("myBody").dataset.homeurl;

//让图片自适应圆形，居中显示，
function divIntoCircle ($referdiv,$div){
    var textHeight = $referdiv;
    var imgheight = $div.height();
    var imgwidth = $div.width();
    var heightprecent = imgwidth/textHeight;
    var widthprecent = textHeight/imgwidth;
    $div.parent().css("height",textHeight).css("width",textHeight);
    //让图片正中显示
    // 当图片宽>高
    if (Math.abs(imgwidth-imgheight)>10 && imgwidth>imgheight){
        $("<img/>").attr("src", $div.attr("src")).load(function(){
            $div
                .css("height","100%")
                .css("margin-left",($div.parent().height()-$div.parent().height()/this.height*this.width)/2 + "px")
                .css("max-width","none");
        })

    }else if(Math.abs(imgwidth-imgheight)>10 && imgwidth<imgheight){
        $div.parent().css("height",textHeight).css("width",textHeight);

        $("<img/>").attr("src", $div.attr("src")).load(function(){
            $div
                .css("width","100%")
                .css("margin-top",($div.parent().height()-$div.parent().width()/this.width*this.height)/2+"px");
        })
    }else if(Math.abs(imgwidth-imgheight)<10){
        $div.css("width","100%")
        return
    }
}
//循环每一个图片，让他自适应居中
function imgnumber(){
    imgNumber = $(".thumbnail").parent().parent().children();
    for (var i = imgNumber.length - 1; i >= 0; i--) {
        divIntoCircle( $(".grid_people .thumbnail").width() , $(".grid_people .thumbnail a img:eq("+i+")") );
    }
}
//投票
function voteClick($div){
    $div.click(function(){
        this_div = $(this);
        var applyid = $(this).attr("id");
        var vote = {
            user_id : userid,
            act_id : actid,
            apply_id : applyid,
            openid:openid
        };
        if(this_div.attr("disabled")=="disabled"){
            return
        }
        this_div.attr("disabled",true);
        $.ajax({
            url:dataRoot+"/Home/Index/addRecord",
            type:'get',
            data:vote,
            success:function(res){
                if(res.request.status=="success"&&res.result.status=="success"){
                    alert("投票成功！");
                    this_div.attr("disabled",false);
                    window.location.href=homeurl+"?time=new Date()";
                }else if(res.request.status=="success"&&res.result.status=="fail"){
                    alert(res.result.info);
                    this_div.attr("disabled",false);
                    if(res.result.errcode==30016){
                        $("#erweima").css('display','block');
                    }
                }else if(res.request.status=="fail"){
                    alert(res.request.info);
                    this_div.attr("disabled",false);
                }
            }
        });
    })
}

$('.footer .list-item').click(function() {
    var index = $(this).index()
    fp_color(index)
})
function fp_color(index) {
    $('.footer .list-item').eq(index).find('a').removeClass('color')
    $('.footer .list-item').eq(index).find('a').addClass('color')
}
$(document).ready(function(){

    $('.btn_upload').change(function(){
        $("#jiazai").css('display','block');
        var form='#'+$(this).parent().parent().attr("id");
        var photo=new FormData($(form)[0]);
        var thisfile = $(this);
        var app = document.getElementById("myBody").dataset.app;
        //当上传图片时候提交按钮设置为disabled
        $(".bt .sub-btn").attr("disabled",true);
        $.ajax({
            url:app,
            type:'post',
            data:photo,
            contentType: false,
            processData: false,
            success:function(res){
                $("#jiazai").css('display','none');
                if(res.status == "fail"){
                    alert('请选择合适大小的照片哦');
                    return
                }
                thisfile.parent().find("img").attr("id", res.picId);
                thisfile.parent().find("img").attr("src",dataRoot + res.path);
                thisfile.parent().parent().parent().next().removeClass("upload-hide");
                thisfile.parent().parent().parent().parent().find(".last-one").change(function(){
                    $(".first-one").removeClass("upload-hide");
                    $(".first-one").parent().addClass("margin_top");
                })
                //当上传图片时候提交按钮设置为disabled
                $(".bt .sub-btn").attr("disabled",false);
            }
        });
    });

    $(".textarea-max").keyup(function(){
        var hasnumber = $(this).val().length;
        var lastnumber = 50- hasnumber ;
        if(hasnumber >= 50){
            $(this).next().html( "*宝宝描述请不要超过50字。")
        }else{
            $(this).next().html( "*还剩下"+ lastnumber +"个字")
        }
    })

    $(".bt .sub-btn").click(function(){
        //必须填写名字
        if ($("#name").val()== "") {
            alert("请输入名字");
            $("#name").focus();
            return
        }
        //年龄必填
        if ($("#age").val()== "") {
            alert("请输入年龄");
            $("#age").focus();
            return
        }
        //地址必填
        if ($("#address").val()== "") {
            alert("请输入地址");
            $("#address").focus();
            return
        }
        //必须填写电话号码
        if ($("#phone").val()== "") {
            alert("请输入手机号");
            $("#phone").focus();
            return
        }

        if( $("#phone").val().length!=11 ){
            //$("#phone").val("");
            alert("手机号码不够11位");
            //$(this).attr("placeholder","请输入正确的手机号码！！！！！！");
            $("#phone").focus();
            return
        }
        var beginNum = $("#phone").val().substring(0,2);
        if( beginNum!=13 && beginNum!=15 && beginNum!=17 && beginNum!=18 ){
            //$("#phone").val("");
            alert("请输入正确的手机号码");
            $("#phone").focus();
            return
        }

        //必须上传至少一张图片
        if ($("#form1 img").attr('src') == "") {
            alert("至少上传一张图片！");
            return
        }
        //内容必填
        if ($(".messages textarea").val() == "") {
            alert("内容必填");
            $(".messages textarea").focus();
            return
        }
        for (var i = 1; i <= 8; i++) {
            imgid = "#form"+i+" img";
            var imgSrc = $(imgid).attr("id");
            if(imgSrc == undefined){
                break;
            }
            imgMsg += imgSrc+",";
        };
        imgMsg = imgMsg.substring(0,(imgMsg.length-1))
        var msg = {
            "name":$("#name").val(),
            "phone":$("#phone").val(),
            "age":$("#age").val(),
            "address":$("#address").val(),
            "picture_id":imgMsg,
            "introduce":$(".textarea-max").val(),
            "act_id":actid,
            "user_id":userid,
            "openid":openid
        };
        $.ajax({
            url:sub,
            type:'get',
            data: msg ,
            success:function(res){
                //alert(res.request.status+' '+res.result.status);
                if(res.result.status == "success"){
                    alert("恭喜你，报名成功！");
                    window.location.href = homeurl+"?time=new Date()";
                }else if(res.result.status == "fail"){
                    alert(res.result.info);
                    if(res.result.errcode==30016){
                        $("#erweima").css('display','block');
                    }
                    return
                }
            }
        });
    })



    var hrefArray =  document.location.href.split("/");
    for (var i = hrefArray.length - 1; i >= 0; i--) {
        if(hrefArray[i] == 'apply'){
            var data_id = {
                "user_id":userid,
                "act_id":actid,
                "openid":openid
            };

            $.ajax({
                url: dataRoot+'/Home/Index/checkApply',
                type:'get',
                data: data_id ,
                success:function(res){
                    if(res.request.status == "success"){
                        if(res.result.status== 'fail' &&( res.result.errcode == 30009 || res.result.errcode == 30010)){
                            alert(res.result.info);
                            window.location.href = homeurl+"?time=new Date()";
                        }else if (res.result.status== "success" && res.result.data!="") {
                            alert("你已经报名过了！");
                            window.location.href = homeurl+"?time=new Date()";
                        }
                    }
                }
            });
            return
        }else if(hrefArray[i] == 'player'){
            divIntoCircle($(".player_minute .player_info .text").height(),$(".player_minute .player_info .img img"));
            voteClick($(".Ta .Ta_t .btn"));
            return
        }else if(hrefArray[i] == 'explain'){
            return
        }else if(hrefArray[i] == 'line'){
            return
        }else if(hrefArray[i] == 'index'){
            $(".search .form-control").keydown(function (e) {
                if (e.keyCode == 13) {
                    window.location.href = homeurl+"?keyword="+$(".search .form-control").val();
                }
            });
            $(window).resize(function(){
                imgnumber();
            })
            $(window).change(function(){
                imgnumber();

            })
            imgnumber();
            voteClick($(".thumbnail .caption .circle"));
            return
        }
    }
});




$(".Ta .Ta_b button").click(function(){
    $("#fenxiang").css("display","block");
})

$("#fenxiang").click(function(){
    $("#fenxiang").css("display","none");
})

$("#erweima").click(function(){
    $("#erweima").css("display","none");
})

